package com.zhouwr.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhouwr.common.core.aspect.annotation.AutoLog;
import com.zhouwr.common.core.util.TreeUtil;
import com.zhouwr.common.core.vo.Query;
import com.zhouwr.common.core.vo.Result;
//import com.zhouwr.common.security.utils.SecurityUtils;
import com.zhouwr.system.entity.PermissionEntity;
import com.zhouwr.system.service.PermissionService;
import com.zhouwr.system.vo.RouterTreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 系统资源
 *
 * @author zhouwr
 * @email 1551541860@qq.com
 * @date 2020-10-08 15:53:25
 */
@Slf4j
@Api(tags = "系统资源")
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    /**
     * 分页列表查询-系统资源
     *
     * @param params 分页查询参数
     * @return Result<IPage < PermissionEntity>>
     */
    @AutoLog(value = "分页列表查询-系统资源")
    @ApiOperation(value = "分页列表查询-系统资源" , notes = "分页列表查询-系统资源")
    @RequestMapping("/list")
    // @RequiresPermissions("system:permission:list")
    public Result<IPage<PermissionEntity>> list(@RequestParam Map<String, Object> params) {
        IPage<PermissionEntity> page = permissionService.page(new Query<PermissionEntity>().getPage(params), new QueryWrapper<PermissionEntity>());
        return Result.ok(page, "分页查询成功！");
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public Result<?> getRouters() {
//        Long userId = SecurityUtils.getLoginUser().getUserId();
        List<PermissionEntity> permissionList = permissionService.list();

        // 转换成树状路由菜单
        List<RouterTreeVo> routerTreeList = TreeUtil.listToTree(
            permissionList.stream().map(menu -> {
                RouterTreeVo routerTree = new RouterTreeVo(menu);
                return routerTree;
            }).collect(Collectors.toList())
        );

        return Result.ok(routerTreeList, "获取路由信息成功！");
    }

    /**
     * 根据id获取-系统资源
     *
     * @param id
     * @return Result<PermissionEntity>
     */
    @AutoLog(value = "根据id获取-系统资源")
    @ApiOperation(value = "根据id获取-系统资源" , notes = "根据id获取-系统资源")
    @RequestMapping("/get/{id}")
    // @RequiresPermissions("system:permission:info")
    public Result<PermissionEntity> info(@PathVariable("id") String id) {
        PermissionEntity permission = permissionService.getById(id);
        return Result.ok(permission, "系统资源" + "信息获取" + (permission != null ? "成功" : "失败"));
    }

    /**
     * 保存-系统资源
     *
     * @param permission
     * @return Result<?>
     */
    @AutoLog(value = "保存-系统资源")
    @ApiOperation(value = "保存-系统资源" , notes = "保存-系统资源")
    @RequestMapping("/save")
    // @RequiresPermissions("system:permission:save")
    public Result<?> save(@RequestBody PermissionEntity permission) {
        boolean b = permissionService.save(permission);
        return Result.ok(b, "系统资源" + "信息保存" + (b ? "成功" : "失败"));
    }

    /**
     * 修改-系统资源
     *
     * @param permission
     * @return Result</ ?>
     */
    @AutoLog(value = "修改-系统资源")
    @ApiOperation(value = "修改-系统资源" , notes = "修改-系统资源")
    @RequestMapping("/update")
    // @RequiresPermissions("system:permission:update")
    public Result<?> update(@RequestBody PermissionEntity permission) {
        boolean b = permissionService.updateById(permission);
        return Result.ok(b, "系统资源" + "信息修改" + (b ? "成功" : "失败"));
    }

    /**
     * 删除-系统资源
     *
     * @param ids 主键数组
     * @return Result<?>
     */
    @AutoLog(value = "删除-系统资源")
    @ApiOperation(value = "删除-系统资源" , notes = "删除-系统资源")
    @RequestMapping("/delete")
    // @RequiresPermissions("system:permission:delete")
    public Result<?> delete(@RequestBody String[] ids) {
        boolean b = permissionService.removeByIds(Arrays.asList(ids));
        return Result.ok(b, "系统资源" + "信息删除" + (b ? "成功" : "失败"));
    }

}
